#include <iostream>
#include <unistd.h>
#include <string>
#include <jsoncpp/json/json.h> //头文件注意！！
using namespace std;

// 序列化
int main()
{
    // Json::Value 对象
    Json::Value root;
    root["one"] = "yi";
    root["two"] = "er";
    root["three"] = "san";

    string str = root.toStyledString();
    cout << str << endl;

    // Json::FastWriter 对象,不添加额外空格、换行符
    Json::FastWriter fast;
    str = fast.write(root);
    cout << str << endl;

    // Json::StreamWriterBuilder
    Json::StreamWriterBuilder stream;
    std::unique_ptr<Json::StreamWriter> writer(stream.newStreamWriter());
    stringstream ss;
    writer->write(root, &ss);
    cout << ss.str() << endl;
}

// 反序列化
int main()
{
    // string str = {"\" one ":\" yi ",\" three ":3,\" two ":" er \""};
    string str = "{\"name\":\"张三\", \"age\":30, \"city\":\"北京\"}";
    Json::Value root;
    Json::Reader read;
    read.parse(str, root);

    cout << root["name"].asString() << endl;
    cout << root["city"].asString() << endl;
    cout << root["age"].asInt() << endl;
}